package com.rattsoftware.client.views;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.View;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.FormButtonBinding;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.HtmlEditor;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.Radio;
import com.extjs.gxt.ui.client.widget.form.RadioGroup;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.form.FormPanel.LabelAlign;
import com.extjs.gxt.ui.client.widget.layout.ColumnData;
import com.extjs.gxt.ui.client.widget.layout.ColumnLayout;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.google.gwt.core.client.GWT;
import com.rattsoftware.client.AppMessages;
import com.rattsoftware.client.events.AppEvents;
import com.rattsoftware.client.model.CourseSummary;
import com.rattsoftware.client.model.DepartmentSummary;
import com.rattsoftware.client.model.TestData;
import com.rattsoftware.client.util.ProgressIndicator;
import com.rattsoftware.server.admin.model.Department;
import com.rattsoftware.server.admin.model.RattsModelConstants;

public class NewEditCourseView extends View {
	final AppMessages appMessages = GWT.create(AppMessages.class);

	private TextField<String> newCourseShortName;
	private TextField<String> newCourseName;
	private HtmlEditor newCourseDescription;
	private ComboBox<DepartmentSummary> departmentNames;
	private NumberField noOfCourseUnits;
	private NumberField noOfSemesters;
	// Course Type
	private RadioGroup courseType;
	private Radio radioDiploma;
	private Radio radioUnderGraduate;
	private Radio radioPostGraduate;

	// Course Type
	private RadioGroup courseDuration;
	private Radio radioYearOne;
	private Radio radioYearTwo;
	private Radio radioYearThree;
	private Radio radioYearFour;
	private Radio radioYearFive;

	private CourseSummary courseSummary;
	
	private Window window;

	public NewEditCourseView(Controller controller) {
		super(controller);
	}

	@Override
	protected void handleEvent(AppEvent event) {
		GWT.log("NewCourseOrCourseUnitView : handleEvent");
		if (event.getType() == AppEvents.NewCourse) {
			createUI("Add New Course");
		} else if (event.getType() == AppEvents.EditCourse) {
			courseSummary = event.getData();
			createUI("Edit Course Details for " + courseSummary.getCourseName());
		}
	}

	private void createUI(String title) {
		window = new Window();
		window.setHeading(title);
		window.setModal(true);
		window.setPlain(true);
		window.setClosable(false);
		window.setMaximizable(false);
		window.setDraggable(true);
		window.setResizable(false);
		window.setSize(745, 495);
		window.setScrollMode(Scroll.AUTOY);
		window.setLayout(new FitLayout());

		ProgressIndicator.showProgressBar();
		window.add(getCoursePanel());
		ProgressIndicator.hideProgressBar();
		window.show();
	}

	public ListStore<DepartmentSummary> getDepartmentStore() {
		List<Department> departments = TestData.getDepartments();
		Collections.sort(departments, new Comparator<Department>() {
			public int compare(Department arg0, Department arg1) {
				return arg0.getName().compareTo(
						arg1.getName());
			}
		});

		ListStore<DepartmentSummary> deptStore = new ListStore<DepartmentSummary>();

		for (Department department : departments) {
			deptStore.add(new DepartmentSummary(department));
		}
		return deptStore;
	}

	private FormPanel getCoursePanel() {
		FormData formData = new FormData("100%");

		FormPanel panel = new FormPanel();
		panel.setFrame(true);
		panel.setHeaderVisible(false);
		panel.setSize(700, -1);
		panel.setLabelAlign(LabelAlign.TOP);

		LayoutContainer main = new LayoutContainer();
		main.setLayout(new ColumnLayout());

		LayoutContainer left = new LayoutContainer();
		left.setStyleAttribute("paddingRight", "10px");
		FormLayout layout = new FormLayout();
		layout.setLabelAlign(LabelAlign.TOP);
		left.setLayout(layout);

		LayoutContainer leftEditor = new LayoutContainer();
		layout = new FormLayout();
		layout.setLabelAlign(LabelAlign.TOP);
		leftEditor.setLayout(layout);

		LayoutContainer right = new LayoutContainer();
		right.setStyleAttribute("paddingLeft", "10px");
		layout = new FormLayout();
		layout.setLabelAlign(LabelAlign.TOP);
		right.setLayout(layout);

		newCourseShortName = new TextField<String>();
		newCourseShortName.setFieldLabel("Course Short Name");

		if (courseSummary != null) {
			newCourseShortName.setValue(courseSummary.getShortName());
		}

		left.add(newCourseShortName, formData);

		newCourseName = new TextField<String>();
		newCourseName.setFieldLabel("Course Name");

		if (courseSummary != null) {
			newCourseName.setValue(courseSummary.getCourseName());
		}
		right.add(newCourseName, formData);

		noOfCourseUnits = new NumberField();
		noOfCourseUnits.setFieldLabel("No. Of Course Units");

		if (courseSummary != null) {
			noOfCourseUnits.setValue(Integer.parseInt(courseSummary.getNoOfCourseUnits()));
		}
		left.add(noOfCourseUnits, formData);

		// course types
		radioDiploma = new Radio();
		radioDiploma.setBoxLabel("Diploma");

		radioUnderGraduate = new Radio();
		radioUnderGraduate.setBoxLabel("Under Graduate");

		radioPostGraduate = new Radio();
		radioPostGraduate.setBoxLabel("Post Graduate");

		if (courseSummary != null) {
			if (courseSummary.getCourseType().equalsIgnoreCase(
					RattsModelConstants.COURSE_DIPLOMA)) {
				radioDiploma.setValue(true);
			} else if (courseSummary.getCourseType().equalsIgnoreCase(
					RattsModelConstants.COURSE_UNDERGRADUATE)) {
				radioUnderGraduate.setValue(true);
			} else if (courseSummary.getCourseType().equalsIgnoreCase(
					RattsModelConstants.COURSE_POSTGRADUATE)) {
				radioPostGraduate.setValue(true);
			}
		} else {
			radioDiploma.setValue(true);
		}

		courseType = new RadioGroup();
		courseType.setFieldLabel("Course Type");
		courseType.add(radioDiploma);
		courseType.add(radioUnderGraduate);
		courseType.add(radioPostGraduate);

		right.add(courseType, formData);

		// Course Duration
		radioYearOne = new Radio();
		radioYearOne.setBoxLabel("1 Year");

		radioYearTwo = new Radio();
		radioYearTwo.setBoxLabel("2 Years");

		radioYearThree = new Radio();
		radioYearThree.setBoxLabel("3 Years");

		radioYearFour = new Radio();
		radioYearFour.setBoxLabel("4 Years");

		radioYearFive = new Radio();
		radioYearFive.setBoxLabel("5 Years");

		if (courseSummary != null) {
			if (Integer.parseInt(courseSummary.getCourseDuration()) == 1) {
				radioYearOne.setValue(true);
			} else if (Integer.parseInt(courseSummary.getCourseDuration()) == 2) {
				radioYearTwo.setValue(true);
			} else if (Integer.parseInt(courseSummary.getCourseDuration()) == 3) {
				radioYearThree.setValue(true);
			} else if (Integer.parseInt(courseSummary.getCourseDuration()) == 4) {
				radioYearFour.setValue(true);
			} else if (Integer.parseInt(courseSummary.getCourseDuration()) == 5) {
				radioYearFive.setValue(true);
			}
		} else {
			radioYearOne.setValue(true);
		}

		courseDuration = new RadioGroup();
		courseDuration.setFieldLabel("Course Duration");
		courseDuration.add(radioYearOne);
		courseDuration.add(radioYearTwo);
		courseDuration.add(radioYearThree);
		courseDuration.add(radioYearFour);
		courseDuration.add(radioYearFive);

		left.add(courseDuration, formData);

		if (courseSummary != null) {
			departmentNames = new ComboBox<DepartmentSummary>();
			departmentNames.setFieldLabel("Choose Department");
			departmentNames.setDisplayField("departmentName");
			departmentNames.setTriggerAction(TriggerAction.ALL);
			departmentNames.setStore(getDepartmentStore());
			//departmentNames.setValue(courseSummary.getDepartmentName());
		} else {
			departmentNames = new ComboBox<DepartmentSummary>();
			departmentNames.setFieldLabel("Choose Department");
			departmentNames.setDisplayField("departmentName");
			departmentNames.setTriggerAction(TriggerAction.ALL);
			departmentNames.setStore(getDepartmentStore());
		}
		
		right.add(departmentNames, formData);

		main.add(left, new ColumnData(.5));
		main.add(right, new ColumnData(.5));

		panel.add(main, new FormData("100%"));

		newCourseDescription = new HtmlEditor();
		newCourseDescription.setFieldLabel("Course Description");
		newCourseDescription.setHeight(200);

		if (courseSummary != null) {
			newCourseDescription.setValue(courseSummary.getCourseDescription());
		}
		leftEditor.add(newCourseDescription, formData);

		panel.add(leftEditor, new FormData("100%"));

		Button buttonSave = new Button(appMessages.save());
		buttonSave.setIcon(IconHelper.createStyle("add"));
		buttonSave.addListener(Events.Select, new Listener<ButtonEvent>() {
			@Override
			public void handleEvent(ButtonEvent be) {
				// addNewCourse();
			}
		});
		
		Button buttonCancel = new Button(appMessages.cancel());
		buttonCancel.setIcon(IconHelper.createStyle("cancel"));
		buttonCancel.addListener(Events.Select, new Listener<ButtonEvent>() {
			@Override
			public void handleEvent(ButtonEvent be) {
				closeWindow();
			}
		});

		panel.addButton(buttonSave);
		panel.addButton(buttonCancel);

		FormButtonBinding binding = new FormButtonBinding(panel);
		binding.addButton(buttonSave);

		return panel;
	}
	
	@SuppressWarnings("deprecation")
	private void closeWindow() {
		courseSummary = null;
		ProgressIndicator.showProgressBar();
		window.close();
		ProgressIndicator.hideProgressBar();
	}
}
